﻿using System;
using System.Web.Caching;
using System.Collections.Generic;

using Fuse8.DomainFramework.Configuration.Cache;

namespace Fuse8.DomainFramework.CacheService
{
    /// <summary>
    /// 
    /// </summary>
    public class CacheServiceOptions
    {
        #region Fields

        private IList<string> _dependencies;
        private readonly IList<IEntityCacheServiceElement> _chainCacheServices = new List<IEntityCacheServiceElement>();

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public DateTime AbsoluteExpiration
        {
            get
            {
            	return AbsoluteTimeSpanExpiration != TimeSpan.Zero ? DateTime.Now.AddMilliseconds(AbsoluteTimeSpanExpiration.TotalMilliseconds) : Cache.NoAbsoluteExpiration;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public TimeSpan AbsoluteTimeSpanExpiration { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public TimeSpan SlidingExpiration { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public TimeSpan TagSlidingExpiration { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public TimeSpan TagAddExpiration { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public CacheItemPriority Priority { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public CacheItemRemovedCallback RemovedCallback { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool UseAsyncMethods { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public IList<string> Dependencies
        {
            internal get
            {
                return _dependencies;
            }
            set
            {
                _dependencies = value ?? new List<string>();

            	foreach (var item in _dependencies)
                {
                	var cacheServiceElement = CacheServiceFactory.GetCacheServiceElement(item);
                	if (cacheServiceElement != null)
                    {
                        _chainCacheServices.Add(cacheServiceElement);
                    }
                }
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public CacheServiceOptions()
        {
            AbsoluteTimeSpanExpiration = TimeSpan.Zero;

            SlidingExpiration = Cache.NoSlidingExpiration;

            TagSlidingExpiration = new TimeSpan(0, 20, 0);

            TagAddExpiration = new TimeSpan(0, 0, 10);

            Priority = CacheItemPriority.Default;

            RemovedCallback = null;

            UseAsyncMethods = true;
        } 

        #endregion

        #region Methods

        internal void AddToDependency(string cacheKey)
        {
            foreach (var item in _chainCacheServices)
            {
                item.AddToDependency(cacheKey);
            }
        }

        #endregion
    }
}
